package com.lhy.controller;

import com.github.pagehelper.PageInfo;
import com.lhy.entity.User;
import com.lhy.service.UserService;
import com.lhy.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getAll")
    public String selectAll(Model model, @RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "3") int pageSize) {
        PageInfo<User> pageInfo = userService.selectAll(current, pageSize);
        //        model.addAttribute("list", list);
        model.addAttribute("pageInfo", pageInfo);
        return "/selectAll.jsp";
    }
    @ResponseBody
    @RequestMapping("/id")
    public User selectById(Integer id,Model model) {
        User user = userService.selectById(id);
        model.addAttribute("id",user);
        return user;
    }

    @RequestMapping("/like")
    @ResponseBody
    public List<User> selectByLike(String like) {
        List<User> users = userService.selectByLike("%光%");

        return users;


    }

    //    @ResponseBody
    @RequestMapping("/insertUser")
    public String insertUser(MultipartFile myfile,User user, Model model) {
        String uploadOss = OssUtils.uploadOss(myfile);
        user.setPicture(uploadOss);
        int i = userService.insertUser(user);
        model.addAttribute("insert", i);

        return "/hhhhh.jsp";
    }

    @RequestMapping("/delete")
    public String deleteUser(Integer id, Model model) {
        int i = userService.deleteUser(id);
        model.addAttribute("delete", i);
        return "/selectAll.jsp";
    }

    @RequestMapping("/update")
    public String updateUser(MultipartFile myfile,User user,Model model){
        String oss = OssUtils.uploadOss(myfile);
        user.setPicture(oss);
        userService.updateUser(user);
        return "/hello.jsp";
    }

}
